#!/usr/bin/env python2.7
# -*- conding: utf-8 -*-
# @Time : 2020/9/21 10:10
# @Author : mj
# @file : check_playbook_res.py
# @Project: python-script
import re

result =''' string(7) "ansible"
ansible-playbook 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: commprepare.yaml *****************************************************
1 plays in deploy_game/commprepare.yaml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
task path: /root/ansible_playbook/deploy_game/commprepare.yaml:2
fatal: [10.0.0.36]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).", "unreachable": true}

PLAY RECAP *********************************************************************
10.0.0.36 : ok=0 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0'''

#print result


def check_playbook_res(newres):
	#print 'enter check playbook res'
	#m = re.match(r'.*unreachable=.*', newres)
	#print m
	p = re.findall(r'.*changed=.*unreachable=.*failed.*', newres)
	print p
	#print type(p)
	#p = ['192.168.130.51             : ok=21   changed=17   unreachable=0    failed=0   ']
	failed_status = False
	if p:
		for k in p:
			if re.findall(r'unreachable=[1-9]+', k) or re.findall(r'failed=[1-9]+', k) or re.findall(r'changed=0',k):
				failed_status  = True
			else:
				success_status = True
	else:
		return False

	if failed_status:
		return False

	return True


#res = check_playbook_res(result)
#print res